package erfgame.frontend.desktop.view.terrain.iso.interaction;

import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import erfgame.core.xml.ObjectXMLFactory;

public class InteractionDescriptionXMLFactory 
	implements ObjectXMLFactory<InteractionDescription> {

	private static final String ELEMENT_INTERACTION_DESCRPTION = "interaction";
	private static final String ELEMENT_DESCRIPTION = "description";
	private static final String ELEMENT_CHILDREN = "children";
	private static final String ATTRIBUTE_NAME = "name";
	private static final String ATTRIBUTE_ID = "id";
	
	public Node toXML(InteractionDescription object, Document document) {
		String description = object.getDescription();
		String name = object.getName();
		List<InteractionDescription> children = object.getChildren();
		
		Element topLevel = document.createElement(ELEMENT_INTERACTION_DESCRPTION);
		
		topLevel.setAttribute(ATTRIBUTE_ID, object.getId());		
		if( name != null ) {
			topLevel.setAttribute(ATTRIBUTE_NAME, name);
		}
		
		if( description != null ) {
			Element descriptionNode = document.createElement(ELEMENT_DESCRIPTION);
			descriptionNode.setNodeValue(description);
			topLevel.appendChild(descriptionNode);
		}

		if( children != null ) {
			Element childrenNode = document.createElement(ELEMENT_CHILDREN);
			for( int i=0; i<children.size(); i++ ) {
				InteractionDescription child = children.get( i );
				childrenNode.appendChild(
					toXML(child, document)	
				);
			}
			topLevel.appendChild(childrenNode);
		}
		
		return topLevel;
	}
}
